<?PHP
session_start();
include("common/connect.php");
include("common/common.php");

if( !isset($_SESSION["user"]) ) 
{
	$url = "Location:login.php?ref=get_recommend_data.php";
	header($url);
}

// 详细检查，避免使用错误
$opt = trim(CheckUrlParamNoDie( "opt" ));
$date1 = trim(CheckUrlParamNoDie( "date1" ));
$date2 = trim(CheckUrlParamNoDie( "date2" ));
if ($date1 == "" or $date2 == "" or $date1 >= $date2)
{
	echo "lost date!";
	echo "please give date1 and date2, date1 <= xx < date2<br>";
	exit;
}

$date_last = "20110101";
$sql = "select date(time) as tmp from recommend_fee order by id desc limit 1";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0)
	$date_last = mysql_result($result, 0, "tmp");
	
if ($opt != "do")
{
	echo "last data time is:".$date_last."<br>";
	echo "please give date1 and date2, date1 <= xx < date2<br>";
	echo "<a href='?opt=do&date1=".$date1."&date2=".$date2."'>start fetch data</a>";
	exit;
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>购物兔</title>
<style type="text/css">
<!--
body,td,th {
	font-family: 宋体;
	font-size: 13px;
	color: #000;
}
body {
	background-color: #FFF;
}
-->
</style>

</head>

<body>
<!--<a href="add_trade.php?opt=add">添加记录</a>-->
<table cellpadding="0" cellspacing="0" border="1" align="center">
	<tr>
    	<td height="25" width="250" align="center">支付宝账号</td>
        <td width="141" align="center">手机号码</td>
        <td width="121" align="center">共推广用户数</td>
      	<td width="198" align="center">推广成功奖励</td>
        <td width="170" 	align="center">推广提成(元)</td>
    </tr>
<?php

function GetRecommendSuccessCnt( $id, &$nAll )
{
	// 简单点，不传进来了
	$date1 = trim(CheckUrlParamNoDie( "date1" ));
	$date2 = trim(CheckUrlParamNoDie( "date2" ));

	$sql = "select sum(p.commission) as tmp,count(u.id) as tmp2 from order_handle_report as p,user as u where u.commend_id='$id' and p.user_id=u.id and date(p.pay_time) >= $date1 and date(p.pay_time) < $date2";
	$result2 = mysql_query( $sql );
	$row2 = mysql_fetch_assoc( $result2 );
	$nAll = $row2["tmp"] * 0.6;
	
	$sql = "select p.user_id from order_handle_report as p,user as u where u.commend_id='$id' and p.user_id=u.id and date(p.pay_time) >= $date1 and date(p.pay_time) < $date2 group by p.user_id";
	$result2 = mysql_query( $sql );
	$SucCnt = mysql_num_rows( $result2 );
	
	return $SucCnt;
}

$sql = "select commend_id,count(*) as tmp from user group by commend_id order by count(*) desc";
$result = mysql_query( $sql );
while( $result && $row = mysql_fetch_assoc( $result ))
{
	$id = $row["commend_id"];
	$num_rows = $row["tmp"];
	if( !is_numeric($id) )
		continue;
		
	$nAll = 0;
	$Cnt = GetRecommendSuccessCnt( $id, &$nAll );
//	if ($Cnt == 0)
//		continue;
	if ($nAll == 0)
		continue;
		
	//
//	if ($num_rows < 50)
		$nAll *= 0.15;
//	else if ($num_rows < 100)	// 加上兔币后，规则越来越复杂，这里都统一简单按15%算吧
//		$nAll *= 0.16;
//	else if ($num_rows < 200)
//		$nAll *= 0.17;
//	else if ($num_rows < 500)
//		$nAll *= 0.18;
//	else if ($num_rows < 1000)
//		$nAll *= 0.19;
//	else if ($num_rows >= 1000)
//		$nAll *= 0.20;
//	else
//	{
//		echo "error";
//		exit;
//	}
	
	if (1) {
	$sql = "select tb_id,phone,type from user where id='$id' limit 1";
	$result2 = mysql_query( $sql );
	$row2 = mysql_fetch_assoc( $result2 );
	
	// 入库
	$sql = "insert into recommend_fee3 (user_id, user_type, tbid, phone, under_users, all_under_users, fee, operater, state) values ($id, '".$row2["type"]."', '".$row2["tb_id"]."', '".$row2["phone"]."', $Cnt, $num_rows, $nAll, '".$_SESSION["user"]."', 0)";
	mysql_query($sql);
	if (mysql_affected_rows() == 0)
	{
		echo "<script>alert('处理过程中发生失败，请告诉程序员，出错代码：1');</script>";
		exit;
	}
	}
?>
<tr>
    <td height="25"  align="center"><?PHP echo $row["commend_id"]; ?></td>
     <td height="25"  align="center">&nbsp;</td>
    <td align="center" title=""><?PHP echo $num_rows; ?></td>
	<td align="center"><?PHP echo $Cnt; ?>&nbsp;</td>
	<td align="center"><?PHP echo $nAll; ?>&nbsp;</td>
</tr>
<?PHP
}
?>
</table>
</body>
</html>
